// ml278
// Copyright (C) 2008 Bjørn Øivind Bjørnsen, Aleksander Gjermundsen, Knut Andre Tidemann, Thomas Hansen
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#include "planetaryoptions.h"

#include <OgreConfigFile.h>

namespace Ogre
{
	PlanetaryOptions::PlanetaryOptions(PlanetarySceneManager *mgr)
	{
		mSceneMgr = mgr;

		setDefaults();
	}
	
	/**
	 * Set default variables.
	 * This should set sane values for the variables that does not have to be unique.
	*/
	void PlanetaryOptions::setDefaults()
	{
		// TODO add the rest of the variables here!
		TileSize = 8;
		PageSize = 8;
		ZoneSize = 8;

		mPageSizeLOD1 = 16;
		mZoneSizeLOD1 = 16;

		mPageUnloadFrequency = 300;
		mPageLoadFrequency = 15;
		
		mTileDebugSquares = false;
		mTerrainScale = 0.1f;
		mPlanetRadius = 100.0f;

		mPageRenderableLODDistance = 500.0f;
		mZoneToPageLODDistance = 1500.0f;

		mHeightmapName = "";
	}

	/**
	 * Loads the given configfile.
	 * fills the local attributes with the information in the configfile.
	 * @param stream DataStreamPtr to the configfile to read
	*/
	void PlanetaryOptions::load(DataStreamPtr &stream)
	{
		ConfigFile cf;
		cf.load(stream);

		TileSize = StringConverter::parseInt(cf.getSetting("TileSize"));
		PageSize = StringConverter::parseInt(cf.getSetting("PageSize"));
		ZoneSize = StringConverter::parseInt(cf.getSetting("ZoneSize"));

		mHeightmapName = cf.getSetting("Heightmap");
		mPlanetName = cf.getSetting("PlanetName");
		mAtmosphere = StringConverter::parseBool(cf.getSetting("Atmosphere"));
		mPlanetMaterial = cf.getSetting("PlanetMaterial");
		mAtmosphereMaterial = cf.getSetting("AtmosphereMaterial");
		
		mTileDebugSquares = StringConverter::parseBool(cf.getSetting("TileDebugSquares"));

		mTerrainScale = StringConverter::parseReal(cf.getSetting("TerrainScale"));
		mPlanetPosition = StringConverter::parseVector3(cf.getSetting("PlanetPosition"));
		mPlanetRadius = StringConverter::parseReal(cf.getSetting("PlanetRadius"));
		
		mPageRenderableLODDistance = StringConverter::parseReal(cf.getSetting("PageToTileLODDistance"));
		mPageSizeLOD1 = StringConverter::parseReal(cf.getSetting("PageSizeLOD1"));

		mZoneToPageLODDistance = StringConverter::parseReal(cf.getSetting("ZoneToPageLODDistance"));
		mZoneSizeLOD1 = StringConverter::parseReal(cf.getSetting("ZoneSizeLOD1"));

		mPageUnloadFrequency = StringConverter::parseInt(cf.getSetting("PageUnloadFrequency"));
		mPageLoadFrequency = StringConverter::parseInt(cf.getSetting("PageLoadFrequency"));

		mNumerOfTilesLODs = StringConverter::parseInt(cf.getSetting("NumerOfTilesLODs"));
		
		mAtmoSizeLOD0 = StringConverter::parseReal(cf.getSetting("AtmoSizeLOD0"));
		mAtmoSizeLOD1 = StringConverter::parseReal(cf.getSetting("AtmoSizeLOD1"));
		mAtmoSizeLOD2 = StringConverter::parseReal(cf.getSetting("AtmoSizeLOD2"));
		
		mAtmoLOD0Distance = StringConverter::parseReal(cf.getSetting("AtmoLOD0Distance"));
		mAtmoLOD1Distance = StringConverter::parseReal(cf.getSetting("AtmoLOD1Distance"));
		mAtmoLOD2Distance = StringConverter::parseReal(cf.getSetting("AtmoLOD2Distance"));
	}
}
